package p3.game;

import java.util.ArrayList;

import p3.structures.Tree;

public class SetOfGames {

	protected ArrayList<GameBoard> games;
	protected int alien_penalty;
	protected int n_loses;
	
	public SetOfGames(int number_of_games){
		games = new ArrayList<>();
		for (int i=0;i<number_of_games;i++) games.add(new GameBoard());
		alien_penalty = 200;
	}
	
	public SetOfGames(SetOfGames sog){
		games = sog.getGames();
		alien_penalty = sog.alien_penalty;
	}
	
	public void randomInit(){ for (GameBoard gb : games) gb.randomInit(); }
	
	public float execute(Tree tree){
		float fitness = 0;
		int prev_time;
		
		n_loses = 0;
		
		for (GameBoard gb : games){
			gb.resetTime();
			while (!gb.isFinished()){
				prev_time = gb.getTime();
				gb.execute(tree);
				if (prev_time == gb.getTime()){
					gb.step();
				}
			}
			fitness += gb.getDistance();
			if (gb.userLose()){ fitness += alien_penalty; n_loses++;}
		}
		System.out.println(n_loses);
		return fitness;
	}
	
	private ArrayList<GameBoard> getGames() { 
		ArrayList<GameBoard> ret = new ArrayList<>();
		for (GameBoard gb : games) ret.add(gb.getCopy());
		return ret;
	}
	
	
	public void setAlienPenalty(int penalty){ alien_penalty = penalty; }
	
	public SetOfGames getCopy(){ return new SetOfGames(this);}
	
	
	
}
